package cn.com.pajx.pajx_visitor_v3.mvp.presenter;

import android.text.TextUtils;

import java.io.File;
import java.util.LinkedHashMap;

import cn.com.pajx.pajx_visitor_v3.api.HttpApiService;
import cn.com.pajx.pajx_visitor_v3.base.BasePresenter;
import cn.com.pajx.pajx_visitor_v3.utils.CommonUtil;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by ruancw on 2024/8/22.
 */

public class LoadDataPresenterImpl extends BasePresenter {

    /**
     * 加载数据
     *
     * @param url             api
     * @param paramsMap       参数集合
     * @param tag             接口标识
     * @param progressMessage 加载提示
     */
    public void loadData(String url, LinkedHashMap<String, String> paramsMap, String tag, String progressMessage) {
        if (!TextUtils.isEmpty(progressMessage)) {
            mView.showProgress(progressMessage);
        }
        String aesKey = CommonUtil.getRandomNonce();
        addSubscription(mModel.getObservable(HttpApiService.getInstance().getRemoteData(url, paramsMap, aesKey), aesKey, tag));
    }

    /**
     * 上传文件
     *
     * @param url             api
     * @param file            文件
     * @param map             参数集合
     * @param tag             请求标识
     * @param progressMessage 加载提示
     */
    public void upLoadFile(String url, File file, LinkedHashMap<String, String> map, String tag, String progressMessage) {
        if (!TextUtils.isEmpty(progressMessage)) {
            mView.showProgress(progressMessage);
        }
        String aesKey = CommonUtil.getRandomNonce();
        //单文件上传
        RequestBody requestBody = RequestBody.create(
                MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData(
                "file", file.getName(), requestBody);
        addSubscription(mModel.getObservable(HttpApiService.getInstance().updateFile(url, part, map, aesKey), aesKey, tag));
    }

    /**
     * 上传文件
     *
     * @param url             api
     * @param file            文件
     * @param map             参数集合
     * @param tag             请求标识
     * @param progressMessage 加载提示
     */
    public void upLoadFile(String url, String paramKey, File file, LinkedHashMap<String, String> map, String tag, String progressMessage) {
        if (!TextUtils.isEmpty(progressMessage)) {
            mView.showProgress(progressMessage);
        }
        String aesKey = CommonUtil.getRandomNonce();
        //单文件上传
        String file_name = file.getName();
        RequestBody requestBody = RequestBody.create(
                MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData(
                paramKey, file_name, requestBody);
        addSubscription(mModel.getObservable(HttpApiService.getInstance().updateFile(url, part, map, aesKey), aesKey, tag));
    }

    /**
     * 上传文件(字节流形式)
     *
     * @param url             api
     * @param fileData        文件字节流
     * @param map             参数集合
     * @param tag             请求标识
     * @param progressMessage 加载提示
     */
    public void upLoadFileBytes(String url, byte[] fileData, LinkedHashMap<String, String> map, String fileName, String tag, String progressMessage) {
        if (!TextUtils.isEmpty(progressMessage)) {
            mView.showProgress(progressMessage);
        }
        String aesKey = CommonUtil.getRandomNonce();
        //单文件上传
        RequestBody requestBody = RequestBody.create(
                MediaType.parse("multipart/form-data"), fileData);
        MultipartBody.Part part = MultipartBody.Part.createFormData(
                "name", fileName, requestBody);
        addSubscription(mModel.getObservable(HttpApiService.getInstance().updateFile(url, part, map, aesKey), aesKey, tag));
    }

    /**
     * 下载文件
     *
     * @param url             下载地址
     * @param map             参数
     * @param tag             标识
     * @param progressMessage 对话框文字
     */
    public void downLoadFile(String url, LinkedHashMap<String, String> map, String tag, String progressMessage) {
        if (!TextUtils.isEmpty(progressMessage)) {
            mView.showProgress(progressMessage);
        }
        String aesKey = CommonUtil.getRandomNonce();
        addSubscription(mModel.getFileObservable(HttpApiService.getInstance().downloadFile(url, map), aesKey, tag));
    }
}
